package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.constant.Constants;
import com.itany.exception.ExamineException;
import com.itany.mapper.BookMapper;
import com.itany.mapper.BookSupplierMapper;
import com.itany.mapper.ExamineMapper;
import com.itany.pojo.Book;
import com.itany.pojo.BookSupplier;
import com.itany.pojo.Examine;
import com.itany.service.ExamineService;
import com.itany.utils.StringUtils;
import com.itany.vo.ExamineVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Author:梁辰
 * 时间:19-4-3 下午6:44
 * description:
 * version:1.0
 */
@Service
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
public class ExamineServiceImpl implements ExamineService {
    @Resource
    private ExamineMapper examineMapper;
    @Resource
    private BookMapper bookMapper;
    @Resource
    private BookSupplierMapper bookSupplierMapper;

    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public PageInfo<Examine> findExamineByFuzzy(Book book, Integer supplierid, Integer page, Integer rows) {
        ExamineVo examineVo=new ExamineVo();
        List<Integer>bookids= new ArrayList<Integer>();
        if (null!=book){
            book.setBookname(StringUtils.formatLike(book.getBookname()));
            book.setISBN(StringUtils.formatLike(book.getISBN()));
            List<Book>books=bookMapper.selectByFuzzy(book);
            for (Book b:books){
                bookids.add(b.getId());
            }
        }
        examineVo.getBookids();
        examineVo.setSupplierid(supplierid);
        PageHelper.startPage(page,rows);
        List<Examine>examines= examineMapper.selectExamineByFuzzy(examineVo);
        PageInfo<Examine>examinePageInfo=new PageInfo<Examine>(examines);
        return examinePageInfo;
    }
    @Override
    public void executeExamine(Examine examine) throws ExamineException {
        Examine byId = examineMapper.selectById(examine.getId());
        if (byId.getFlag()!= Constants.DEFAULT_EXAMINE_FLAG){

            throw new ExamineException("已审核过,无法二次审核");
        }
        examineMapper.updateFlgById(examine);
        Book book=bookMapper.selectById(examine.getBookid());
        book.setNumber(book.getNumber()+examine.getNumber());
        bookMapper.updateNumById(book);
        BookSupplier bookSupplier= new BookSupplier();
        bookSupplier.setBookid(examine.getBookid());
        bookSupplier.setSupplierid(examine.getSupplierid());
        BookSupplier bs =bookSupplierMapper.selectByBookidAndSupplierid(bookSupplier);
        if(null !=bs){
            bookSupplier.setNumber((bs.getNumber()+examine.getNumber()));
            bookSupplierMapper.updateBookSupplier(bookSupplier);

        }else {
            bookSupplier.setNumber(examine.getNumber());
            bookSupplierMapper.insertBookSupplier(bookSupplier);
        }

    }


}
